<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no">
  <title>Line animation</title>
  <style>
    html,
    body,
    #viewDiv {
      padding: 0;
      margin: 0;
      height: 100%;
      width: 100%;
    }
  </style>

  <script>
    var dojoConfig = {
      paths: {
        modules: location.pathname.replace(/\/[^/]+$/, "")
      }
    };
  </script>

  <link rel="stylesheet" href="https://js.arcgis.com/4.3/esri/css/main.css">
  <script src="https://js.arcgis.com/4.3/"></script>

  <script>
    require([
      "esri/Map",
      "esri/views/MapView",
      "esri/layers/FeatureLayer",
      "esri/geometry/Point",
      "esri/geometry/Polyline",
      "esri/geometry/geometryEngine",
      "esri/renderers/SimpleRenderer",
      "esri/symbols/SimpleLineSymbol",
      "esri/symbols/SimpleMarkerSymbol",
      "esri/Graphic",
      "modules/lineAnimation",
      "dojo/domReady!"
    ], function(Map, MapView, FeatureLayer, Point, Polyline, ge,
                SimpleRenderer, SimpleLineSymbol, SimpleMarkerSymbol,
                Graphic, lineAnimation) {

      var map = new Map({
        basemap: "streets"
      });

      var view = new MapView({
        container: "viewDiv",
        map: map,
        zoom: 4,
        center: [15, 65]
      });

      function getAllFeatures (layer){
        return layer.load().queryFeatures()
          .then(function(response){
            return response.features;
          });
      }

      var pointCount = 0;
      var startPoint, endPoint;
      function getPoint(evt){
        view.hitTest(evt).then(function(r){
          var graphic;
          if(r.results.length > 0){
            graphic = r.results[0].graphic;

            if(graphic.geometry.type === "polyline"){
              lineAnimation.unanimateLine({
                lineGraphic: graphic,
                view: view,
//                duration: 300
              });
            }
          }
          return graphic;
        }).then(function(lineResult){
          if(lineResult){
            return;
          }

          var point = new Graphic({
            geometry: evt.mapPoint,
            symbol: new SimpleMarkerSymbol({
              color: "white",
              style: "none",
              outline: {
                color: "black",
                width: 1
              }
            })
          });
          pointCount++;

          startPoint = pointCount === 1 ? point : startPoint;
          endPoint = pointCount === 2 ? point : endPoint;

          view.graphics.add(point);

          if(pointCount === 2){
            pointCount = 0;
            var line = lineAnimation.animateLine({
              startPoint: startPoint,
              endPoint: endPoint,
              view: view,
              duration: 1000
            }).then(function(result){
              console.log("final line: ", result);
            }).otherwise(function(e){
              console.log("final line error: ", e);
            });
            startPoint = null;
            endPoint = null;
          }

        });


      }

      view.on("click", getPoint);

    });
  </script>
</head>

<body>
  <div id="viewDiv"></div>
</body>
</html>